草庐IT

c++ - Clang、std::next、libstdc++ 和 constexpr-ness

全部标签

c++ - 在主程序退出期间销毁等待 std::condition_variable 的线程的正确方法

我正在使用std::conditional_variable为多线程程序中的信号计时,以控制各个关键部分的流程。该程序可以运行,但在退出期间我不得不使用谓词(kill_==true)来避免破坏仍在等待std::conditional_variable::wait()的线程。我不知道它是否是销毁所有等待线程的正确方法,征求意见。这是一个代码片段:classtimer{//...timer(std::shared_ptrparent,constbool&kill):parent_(parent),kill_(kill){}private:std::condition_variablecv_

c++ - 带有自定义删除器的 std::shared_ptr 的 Typedef 别名

我想为std::shared_ptr创建别名使用自定义删除器。此代码有效,但仅适用于唯一指针。我收到有关标有[1]的行的无效模板参数数量的错误。我注意到std::unique_ptr的模板和ctor参数和std::shared_ptr与所列不同here和here我注意到这个问题可能与this重复,但我不知道如何解决我的问题#include#includetemplatestructDeleter{voidoperator()(T*p)constnoexcept{p->Drop();//SFINAE};};templateusingmy_unique_ptr=std::unique_pt

c++ - 未定义对 clang 中函数指针的变量模板的引用,但不是 gcc

#includestaticconstexprboolisSSE2=true;templatestaticvoid(*fp)();templatestaticvoidfoo_c(){std::coutstaticvoidfoo_sse2(){std::cout=foo_sse2;elsefp=foo_c;fp();return0;}我有一个使用变量模板的项目,它本身就是一个指向函数的指针。上面的示例代码在GCC6.3中编译和执行良好,但在clang3.9.1中给出警告和错误。$clang++"Source.cpp"-o"foo.exe"-std=c++14-O2Source.cpp:6

c++ - 在此上下文中的完美转发和 std::move 行为

我是C++新手,我想了解完美转发如何与std::move结合使用.我定义了一个std::vectorqueue()我想使用模板函数填充fillWithData.由于我花了一些时间研究完美转发,所以我首先要检查我是否理解正确,其次要弄清楚move是什么。在此上下文中的行为。fillWithData是一个可变参数模板函数,感谢forward,能够通过折叠规则将参数视为左值或右值。(Q1-是否正确?)templatestaticvoidfillWithData(Container&oDataContainer,Args&&...args)//universalreference{typede

c++ - 具有未触及的非 constexpr 参数 : Who is correct, clang 或 gcc 的 constexpr?

我有4个测试用例,我相信它们都是有效的:constexprintf(intconst&/*unused*/){return1;}voidg(intconst&p){constexprinta=f(p);//clangerror,gccvalidintv=0;constexprintb=f(v);//clangvalid,gccvalidintconst&r=v;constexprintc=f(r);//clangerror,gccerrorintn=p;constexprintd=f(n);//clangvalid,gccvalid}intmain(){intp=0;g(p);}Cla

c++ - static_cast<T&&>(t) 编译速度比 std::forward<T>(t) 快?

最近,我在这里阅读了range-v3的提交评论:https://github.com/ericniebler/range-v3/commit/a4829172c0d6c43687ba213c54f430202efd7497提交消息说,marginallyimprovecompiletimesbyreplacingstd::forwardwithstatic_cast我知道std::forward(t)返回static_cast(t),按照标准。我也知道有时static_cast(t)当T&&t时会正常工作是通过引用折叠规则的通用引用(或转发引用)。我感兴趣的是提交消息说static_c

c++ - std::vector 手动中毒

在下面的代码片段中,有一个错误不是微不足道的,但我希望像AddressSanitizer这样的工具能够捕捉到它。#include#includeintmain(){std::vectortoto;toto.push_back(2);intconst&titi=toto[0];toto.pop_back();std::cout当在vector范围内打印并在范围外打印catch引用时,会抛出use-heap-after-free错误。但是当没有作用域时,std::vector实现可能不会在pop_back之后释放内存,因此引用仍然指向有效内存。我四处搜索,发现您可以手动毒化内存,我想知道这

c++ - 为什么 std::scan_is 在 vi​​sual studio 编译器中发出运行时错误?

示例here在VisualStudio2013中发出内存访问冲突的运行时错误。#include#include#includeintmain(){auto&f=std::use_facet>(std::locale(""));//skipuntilthefirstletterchars1[]="\t\t\nTest";constchar*p1=f.scan_is(std::ctype_base::alpha,std::begin(s1),std::end(s1));std::cout这是为什么呢?编译器的错误实现? 最佳答案 aut

c++ - SFINAE 在评估模板参数中的 constexpr 时失败?

出于某种原因,此constexpr在模板参数上下文中未被正确评估:#include#includenamespacedetail{//Reasontouseanenumclassrahterthanjustanintissoastoensure//therewillnotbeanyclashesresultinginanambigiousoverload.enumclassenabler{enabled};}#defineENABLE_IF(...)std::enable_if_t=detail::enabler::enabled#defineENABLE_IF_DEFINITION(

c++ - 为什么 std::forward 将左值和右值转换为右值引用?

我想我对std::forward感到困惑.我的函数使用std::forward如下,但为了便于解释,它进行了很多简化和修改。//Thisisanexamplecodetoexplainmyquestionsimply.templatevoidadd(Element&&element){staticstd::vectorvec;vec.push_back(std::forward(element));}我用上面的函数尝试了两种情况;Case1左值参数和Case2右值参数。案例1:左值参数autosome_class=SomeClass();add(some_class);案例2:右值参数